<?php
namespace app\api\controller;
use app\common\BaseController;
use app\api\model\Transfer as TransferModel;
use think\Db;
use app\common\Logs;
class Transfer extends BaseController{
    public function __construct(TransferModel $transferModel){
        parent::__construct();
        $this->model = $transferModel;
    }
    public function index(){
        $params = $this->request->param();
        $data = $this->model->transferList($params);
        return $data;
    }    
    //添加
    public function create(){
        $param = $this->request->param();       
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true);                
        $from_bank=Db::name("Bank")->where('id',$params['from_bank'])->value('bank_name');
        $to_bank=Db::name("Bank")->where('id',$params['to_bank'])->value('bank_name');
        $params['title']=$from_bank."转到".$to_bank;
        $data = $this->model->allowField(true)->data($params)->save();
        if($data){
            Logs::add("新增转款记录",$operator_id,"转款", null,json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        }
        return $this->model;
    }
    //更新
    public function update(){
        $param = $this->request->param();       
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true);      
        $from_bank=Db::name("Bank")->where('id',$params['from_bank'])->value('bank_name');
        $to_bank=Db::name("Bank")->where('id',$params['to_bank'])->value('bank_name');
        $params['title']=$from_bank."转到".$to_bank;  
        $old = $this->model->where("id",$params['id'])->find(); 
        $data = $this->model->allowField(true)->save($params,['id',$params['id']]);
        if($data){
            Logs::add("修改转款记录",$operator_id,"转款", json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        }
        return $this->model;
    }
    //删除
    public function delete(){
        $params = $this->request->param();
        $old = $this->model->where("id",$params['id'])->find();   
        $data = $this->model->allowField(true)->where('id',$params['id'])->delete();
        if($data){
            Logs::add("删除转款记录",$params['operator_id'],"转款", json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        } 
        return $data;
    }
}
